#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stdbool.h>
using namespace std;




struct TreeNode
{
	int _val;
	TreeNode* _left;
	TreeNode* _right;
};

TreeNode* TreeCreat()
{
	int num;
	scanf("%d", &num);
	if (num == 0)
		return nullptr;
	TreeNode* root = new TreeNode;
	root->_val = num;
	root->_left = TreeCreat();
	root->_right = TreeCreat();
	return root;
}

void InOrder(TreeNode* root)
{
	if (!root)
		return;
	InOrder(root->_left);
	cout << root->_val << ' ';
	InOrder(root->_right);
}

void Destroy(TreeNode*& root)
{
	if (root == nullptr)
		return;
	Destroy(root->_left);
	Destroy(root->_right);
	delete root;
	root = nullptr;
}

bool EraseTree(TreeNode*& root, int num)
{
	if (root == nullptr || num == 0)
		return false;
	if (root->_val == num)
	{
		Destroy(root);
		return true;
	}
	if (EraseTree(root->_left, num))
		return true;
	return EraseTree(root->_right, num);
}

int main()
{
	TreeNode* root = TreeCreat();
	int m;
	cin >> m;
	while (m--)
	{
		int n;
		cin >> n;
		if (EraseTree(root, n))
		{
			InOrder(root);
			cout << endl;
		}
		else
		{
			cout << 0 << endl;
		}
	}
	return 0;
}